package kt.com.bilin.coin.ui.activity

import android.view.View
import android.view.ViewGroup
import com.bilin.coin.R
import com.bilin.coin.databinding.ItemMyMessageBinding
import kotlinx.android.synthetic.main.activity_base_refresh.*
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.toData
import kt.com.bilin.coin.bean.UserMsgBean
import kt.com.bilin.coin.common.changeVisibility
import kt.com.bilin.coin.common.getString
import kt.com.bilin.coin.ui.adapter.BaseRecyclerAdapter
import kt.com.bilin.coin.ui.adapter.BaseViewHolder

class MyMessageActivity : BaseRefreshActivity<UserMsgBean>() {
    override fun initView() {
        titleView.titleStr = R.string.title_my_message.getString()
        mBaseRecyclerAdapter = MyMessageAdapter()
        super.initView()
    }

    override fun onReload() {
        launchUI({ API_SERVICE.replyList(mRefreshHelper.page).toData()?.records }, mRefreshHelper.handApiObserver())
    }

    inner class MyMessageAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemMyMessageBinding>, UserMsgBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemMyMessageBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemMyMessageBinding>, bean: UserMsgBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    tvTitle.text = replyTitle
                    txMsgDate.text = replyTime
                    tvContent.text = replyContent
                    tvIsRead.changeVisibility = 0 == isRead
                    setViewClick(position, root)
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            val mUserMsgBean = getItem(position)
            if (mUserMsgBean.isRead == 0) {
                launchUI({ API_SERVICE.updateUserMsgRead(mUserMsgBean.id) }, ApiObserver())
                mUserMsgBean.isRead = 1
                notifyItemChanged(position)
            }
            MessageDetailsActivity.open(mUserMsgBean, this@MyMessageActivity)
        }
    }

}
